package com.daisuxia.back.controller;

import static com.daisuxia.web.pojo.BorrowOrder.STATUS_BFHK;
import static com.daisuxia.web.pojo.BorrowOrder.STATUS_HKZ;
import static com.daisuxia.web.pojo.BorrowOrder.STATUS_YHZ;
import static com.daisuxia.web.pojo.BorrowOrder.STATUS_YYQ;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.daisuxia.back.service.TaskJob;
import com.daisuxia.back.utils.DwzResult;
import com.daisuxia.back.utils.ExcelUtil;
import com.daisuxia.back.utils.SpringUtils;
import com.daisuxia.back.utils.SysCacheUtils;
import com.daisuxia.constant.Constant;
import com.daisuxia.web.controller.BaseController;
import com.daisuxia.web.dao.IChannelReportDao;
import com.daisuxia.web.pojo.BackConfigParams;
import com.daisuxia.web.pojo.BackUser;
import com.daisuxia.web.pojo.BorrowOrder;
import com.daisuxia.web.pojo.ChannelInfo;
import com.daisuxia.web.pojo.ChannelReport;
import com.daisuxia.web.pojo.ThirdChannelReport;
import com.daisuxia.web.pojo.User;
import com.daisuxia.web.service.IChannelInfoService;
import com.daisuxia.web.service.IChannelReportService;
import com.daisuxia.web.service.ILoanReportService;
import com.daisuxia.web.service.IThirdChannelReportService;
import com.daisuxia.web.service.IUserService;
import com.daisuxia.web.util.Base64Utils;
import com.daisuxia.web.util.ConfigConstant;
import com.daisuxia.web.util.DateUtil;
import com.daisuxia.web.util.PageConfig;
import com.daisuxia.web.util.QrCodeUtil;
import com.daisuxia.web.util.encrypt.AESUtil;
import com.daisuxia.web.util.encrypt.MD5coding;

@Controller
@RequestMapping("thirdChannel/")
public class ThirdChannelController extends BaseController {

	private static Logger logger = LoggerFactory
			.getLogger(ThirdChannelController.class);

	@Autowired
	IThirdChannelReportService thirdChannelReportService;
	

	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param redenvelope
	 * @param model
	 */

	@RequestMapping("getthirdChannelReportPage")
	public String getChannelReportPage(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		try {
			HashMap<String, Object> params = getParametersO(request);
			BackUser backUser = this.loginAdminUser(request);
			
			PageConfig<ThirdChannelReport> pageConfig = thirdChannelReportService.findPage(params);
			model.addAttribute("pm", pageConfig);
			model.addAttribute("params", params);// 用于搜索框保留值

		} catch (Exception e) {
			logger.error("getChannelReportPage error", e);
		}
		return "thirdChannel/thirdChannelReportPage";
	}
	@RequestMapping("toAgainThirdChannelReport")
	public String toAgainChannelReport(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Map<String, String> params =this.getParameters(request);
		model.addAttribute("params", params);
		String url=null;
		try {
			if("toJsp".equals(String.valueOf(params.get("type")))){
				url="thirdChannel/againThirdReport";
			}else{
				if(params.get("nowTime")!=null){
					thirdChannelReportService.pushUserReport(String.valueOf(params.get("nowTime")));
				 }
				 SpringUtils.renderDwzResult(response, true,"操作成功，如要继续操作，请五分钟后再进行！", DwzResult.CALLBACK_CLOSECURRENT,params.get("parentId")
							.toString());
			}
		} catch (Exception e) {
			logger.error("againReport error", e);
		}
		return url;
	}
	
	@RequestMapping("toAgainThirdPushReport")
	public String toAgainThirdPushReport(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Map<String, String> params =this.getParameters(request);
		model.addAttribute("params", params);
		String url=null;
		try {
			if("toJsp".equals(String.valueOf(params.get("type")))){
				url="thirdChannel/againThirdPushReport";
			}else{
				if(params.get("nowTime")!=null){
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Calendar calendardate = Calendar.getInstance();
					Date sdfdate = sdf.parse(params.get("nowTime"));
					calendardate.setTime(sdfdate);
					calendardate.add(Calendar.DATE,1);
					Date yestDate=calendardate.getTime();
					String yestStr=sdf.format(yestDate);
					
					thirdChannelReportService.saveThirdChannelReport(yestStr);
				 }
				 SpringUtils.renderDwzResult(response, true,"操作成功，如要继续操作，请五分钟后再进行！", DwzResult.CALLBACK_CLOSECURRENT,params.get("parentId")
							.toString());
			}
		} catch (Exception e) {
			logger.error("againReport error", e);
		}
		return url;
	}
	
	@RequestMapping("toAgainThirdUser")
	public String toAgainThirdUser(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Map<String, String> params =this.getParameters(request);
		model.addAttribute("params", params);
		String url=null;
		try {
			if("toJsp".equals(String.valueOf(params.get("type")))){
				url="thirdChannel/againThirdUser";
			}else{
				if(params.get("nowTime")!=null){
					thirdChannelReportService.pushAginUserReport(String.valueOf(params.get("nowTime")));
				 }
				 SpringUtils.renderDwzResult(response, true,"操作成功，如要继续操作，请五分钟后再进行！", DwzResult.CALLBACK_CLOSECURRENT,params.get("parentId")
							.toString());
			}
		} catch (Exception e) {
			logger.error("againReport error", e);
		}
		return url;
	}

	/**
	 * 封装map存入request中
	 * 
	 * @param map
	 * @param request
	 */

	public void setParameters(Map map, HttpServletRequest request) {
		if (request == null) {
			request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
		}
		if (map != null) {
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				Object value = map.get(key);
				request.setAttribute(key, value);
			}
		}
	}
}
